﻿//Copyright (c) 2010, Andreas Grünwald
//All rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections.ObjectModel;
using System.Windows.Input;
using yaTDL.Core;

namespace yaTDL.Core.MVVM
{
    public class SyncConflictViewModel : ViewModelBase
    {

        #region Fields

        public event EventHandler ConflictResolved;

        RelayCommand _selectCommand;

        ObservableCollection<object> _items = new ObservableCollection<object>();
        bool _itemTypeIsTask;
        
        #endregion

        #region Properties

        public ICommand SelectCommand
        {
            get
            {
                if (_selectCommand == null)
                {
                    _selectCommand = new RelayCommand(p => this.selectCommandExecute(p), p => this.selectCommandCanExecute(p));
                }
                return _selectCommand;
            }
        }


        public ObservableCollection<object> Items
        {
            get { return _items; }
            set { _items = value; }
        }

        public bool ItemTypeIsTask
        {
            get
            { return _itemTypeIsTask; }
            set
            {
                if (value != _itemTypeIsTask)
                {
                    _itemTypeIsTask = value;
                    RaisePropertyChanged("ItemTypeIsTask");
                    RaisePropertyChanged("ItemTypeIsProject");
                }
            }
        }

        public bool ItemTypeIsProject
        {
            get
            { return !_itemTypeIsTask; }
        }

        public string DialogTitle { get { return "resolve sync conflict"; } }

        public object SelectedItem { get; set; }        

        #endregion                     


        #region Constructor and private methods
                
        public SyncConflictViewModel(List<object> conflictingItems)
        {

            if (conflictingItems.Any())
            {
                ItemTypeIsTask = conflictingItems[0] is Task ? true : false;
                
                foreach (object o in conflictingItems)
                {
                    Items.Add(o);
                }
            }

            
        }


        void OnConflictResolved()
        {
            if (ConflictResolved != null)
            {
                ConflictResolved(sender: this, e: EventArgs.Empty);
            }
        }

        #endregion

        #region CommandMethods

        bool selectCommandCanExecute(object parameter)
        {
            return SelectedItem == null ? false : true;
        }

        void selectCommandExecute(object parameter)
        {            
            this.CloseView();
        }

        #endregion

        
    }
}
